import datetime
import json
import pandas as pd
import xarray as xr


def check_time_interval(start_time_str, end_time_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    start_time = datetime.datetime.strptime(start_time_str, time_format)
    end_time = datetime.datetime.strptime(end_time_str, time_format)

    # 计算时间间隔
    time_difference = end_time - start_time

    # 定义 36 天 21 小时的时间间隔
    target_interval = datetime.timedelta(days=6, hours=21)

    # 检查时间间隔是否大于或等于 36 天 21 小时
    if time_difference >= target_interval:
        return True
    else:
        return False


def move_time_start_back_365_days_day_format(time_start_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    time_start = datetime.datetime.strptime(time_start_str, time_format)

    # 将时间向前移动 30 天
    new_time_start = time_start - datetime.timedelta(days=365)

    # 将新的时间转换为原来的格式
    new_time_start_str = new_time_start.strftime(time_format)

    return new_time_start_str


def move_time_start_back_30_days(time_start_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    time_start = datetime.datetime.strptime(time_start_str, time_format)

    # 将时间向前移动 30 天
    new_time_start = time_start - datetime.timedelta(days=30)

    # 将新的时间转换为原来的格式
    new_time_start_str = new_time_start.strftime(time_format)

    return new_time_start_str


def move_time_start_back_365_days(time_start_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    time_start = datetime.datetime.strptime(time_start_str, time_format)

    # 将时间向前移动 365 天
    new_time_start = time_start - datetime.timedelta(days=365)

    # 将新的时间转换为原来的格式
    new_time_start_str = new_time_start.strftime(time_format)

    return new_time_start_str


def move_time_start_forward_6_days_and_21_hours(time_start_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    time_start = datetime.datetime.strptime(time_start_str, time_format)

    # 将时间向前移动天
    new_time_start = time_start + datetime.timedelta(days=9, hours=21)

    # 将新的时间转换为原来的格式
    new_time_start_str = new_time_start.strftime(time_format)

    return new_time_start_str


def move_time_start_forward_6_days(time_start_str):
    # 定义时间格式
    time_format = "%Y-%m-%d-%H"

    # 将字符串转换为 datetime 对象
    time_start = datetime.datetime.strptime(time_start_str, time_format)

    # 将时间向前移动 30 天
    new_time_start = time_start + datetime.timedelta(days=9)

    # 将新的时间转换为原来的格式
    new_time_start_str = new_time_start.strftime(time_format)

    return new_time_start_str


def trans_to_hourly(df):
    basin = df["basin"].iloc[0]
    time_hourly = pd.date_range(start=df["time"].min(), end=df["time"].max(), freq="h")
    df_hourly = pd.DataFrame({"time": time_hourly})
    df_hourly = pd.merge(df_hourly, df[["time", "streamflow"]], on="time", how="left")
    df_hourly["streamflow"] = df_hourly["streamflow"].interpolate(
        method="linear"
    )  # 线性插值
    df_hourly["basin"] = basin
    df_hourly["streamflow"] = df_hourly["streamflow"] / 3
    return df_hourly


def get_basin_area(basin_id):
    area = xr.open_dataset("data/attributes.nc").sel(basin=basin_id)["area"].values
    return area
